package com.gyc.game.cchess.model;

import static com.gyc.game.cchess.model.ChessBoardBitPlanes.B_KING;
import static com.gyc.game.cchess.model.ChessBoardBitPlanes.R_KING;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadFactory;

import android.os.Handler;

public abstract class SearchEngineAbstract {

	ChessMove bestMove;

	List<ChessMove> bestMoveList = new ArrayList<ChessMove>();

	ChessBoardBitPlanes chessBoardBitPlanes;

	ChessBoardBitPlanes CurPosition;

	Eveluator eveluator;

	int searchDepth;

	private Thread thread;

	/**
	 * @return the bestMoveList
	 */
	public List<ChessMove> getBestMoveList() {
		return bestMoveList;
	}

	/**
	 * @param bestMoveList
	 *            the bestMoveList to set
	 */
	public void setBestMoveList(List<ChessMove> bestMoveList) {
		this.bestMoveList = bestMoveList;
	}

	/**
	 * @return the thread
	 */
	public Thread getThread() {
		return thread;
	}

	/**
	 * @param thread
	 *            the thread to set
	 */
	public void setThread(Thread thread) {
		this.thread = thread;
	}

	public abstract ChessMove SearchAGoodMove(ChessBoardBitPlanes position);

	/**
	 * @return the uihandler
	 */
	public Handler getUihandler() {
		return uihandler;
	}

	/**
	 * @param uihandler
	 *            the uihandler to set
	 */
	public void setUihandler(Handler uihandler) {
		this.uihandler = uihandler;
	}

	private Handler uihandler;

	public SearchEngineAbstract() {
		super();

	}

	/**
	 * @return the curPosition
	 */
	public ChessBoardBitPlanes getCurPosition() {
		return CurPosition;
	}

	/**
	 * @param curPosition
	 *            the curPosition to set
	 */
	public void setCurPosition(ChessBoardBitPlanes curPosition) {
		CurPosition = curPosition;
	}

	/**
	 * @return the eveluator
	 */
	public Eveluator getEveluator() {
		return eveluator;
	}

	/**
	 * @param eveluator
	 *            the eveluator to set
	 */
	public void setEveluator(Eveluator eveluator) {
		this.eveluator = eveluator;
	}

	/**
	 * @return the searchDepth
	 */
	public int getSearchDepth() {
		return searchDepth;
	}

	/**
	 * @param searchDepth
	 *            the searchDepth to set
	 */
	public void setSearchDepth(int searchDepth) {
		this.searchDepth = searchDepth;
	}

	/**
	 * @return the bestMove
	 */
	public ChessMove getBestMove() {
		return bestMove;
	}

	/**
	 * @param bestMove
	 *            the bestMove to set
	 */
	public void setBestMove(ChessMove bestMove) {
		this.bestMove = bestMove;
	}

	/**
	 * @return the chessBoardBitPlanes
	 */
	public ChessBoardBitPlanes getChessBoardBitPlanes() {
		return chessBoardBitPlanes;
	}

	/**
	 * @param chessBoardBitPlanes
	 *            the chessBoardBitPlanes to set
	 */
	public void setChessBoardBitPlanes(ChessBoardBitPlanes chessBoardBitPlanes) {
		this.chessBoardBitPlanes = chessBoardBitPlanes;
	}

	/**
	 * @param whoturn
	 *            whoturn%2=0 为黑方
	 * @return
	 */
	public int IsGameOver(int whoturn) {

		byte i, j;
		boolean RedLive = false;
		boolean BlackLive = false;

		for (i = 7; i < 10; i++) {
			for (j = 3; j < 6; j++) {
				if (chessBoardBitPlanes.bp_all.getXY(i, j) == B_KING) {
					BlackLive = true;
				} else if (chessBoardBitPlanes.bp_all.getXY(i, j) == R_KING) {
					RedLive = true;
				}
			}
		}

		for (i = 0; i < 3; i++) {
			for (j = 3; j < 6; j++) {
				if (chessBoardBitPlanes.bp_all.getXY(i, j) == B_KING) {
					BlackLive = true;
				} else if (chessBoardBitPlanes.bp_all.getXY(i, j) == R_KING) {
					RedLive = true;
				}

			}
		}

		i = (byte) ((whoturn + 1) % 2);

		int nDepth = this.searchDepth - whoturn;

		if (!RedLive) {
			if (i != 0) {
				return 19990 + nDepth;
			} else {
				return -19990 - nDepth;
			}
		} else if (!BlackLive) {
			if (i != 0) {
				return -19990 - nDepth;
			} else {
				return 19990 + nDepth;
			}
		}

		return 0;
	}

	public abstract void destroy();

}
